package com.pig4cloud.pig.environment.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.environment.entity.SubstationEntity;
import com.pig4cloud.pig.environment.service.SubstationService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 子站管理
 *
 * @author pig
 * @date 2023-12-18 00:02:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/substation" )
@Tag(description = "substation" , name = "子站管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SubstationController {

    private final  SubstationService substationService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param substation 子站管理
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('environment_substation_view')" )
    public R getSubstationPage(@ParameterObject Page page, @ParameterObject SubstationEntity substation) {
        LambdaQueryWrapper<SubstationEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(substation.getName()),SubstationEntity::getName,substation.getName());
		wrapper.like(StrUtil.isNotBlank(substation.getAddress()),SubstationEntity::getAddress,substation.getAddress());
		wrapper.eq(Objects.nonNull(substation.getType()),SubstationEntity::getType,substation.getType());
        return R.ok(substationService.page(page, wrapper));
    }


    /**
     * 通过id查询子站管理
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('environment_substation_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(substationService.getById(id));
    }

    /**
     * 新增子站管理
     * @param substation 子站管理
     * @return R
     */
    @Operation(summary = "新增子站管理" , description = "新增子站管理" )
    @SysLog("新增子站管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('environment_substation_add')" )
    public R save(@RequestBody SubstationEntity substation) {
        return R.ok(substationService.save(substation));
    }

    /**
     * 修改子站管理
     * @param substation 子站管理
     * @return R
     */
    @Operation(summary = "修改子站管理" , description = "修改子站管理" )
    @SysLog("修改子站管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('environment_substation_edit')" )
    public R updateById(@RequestBody SubstationEntity substation) {
        return R.ok(substationService.updateById(substation));
    }

    /**
     * 通过id删除子站管理
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除子站管理" , description = "通过id删除子站管理" )
    @SysLog("通过id删除子站管理" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('environment_substation_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(substationService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param substation 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('environment_substation_export')" )
    public List<SubstationEntity> export(SubstationEntity substation,Long[] ids) {
        return substationService.list(Wrappers.lambdaQuery(substation).in(ArrayUtil.isNotEmpty(ids), SubstationEntity::getId, ids));
    }
}